#-*- encoding:utf8 -*-
from django.contrib.auth.models import User
from django.utils.importlib import import_module
from django.template.loader import get_template
import re

class Tools(object):
    @classmethod
    def analysis_datatable_post(cls,request):
        result = {
            u'columns' : {},
            u'orders' : {},
            u'draw' : 0,
            u'length' : 0,
            u'start' : 0
        }

        def columns_data(key,value,match_obj):
            num = match_obj.groups()[0]
            result[u'columns'][num] = value

        def order_column(key,value,match_obj):
            num = match_obj.groups()[0]
            orders = result[u'orders'].get(num,{})
            orders[u'name'] = request.POST.get(u'columns[%s][data]'%value)
            result[u'orders'][num] = orders

        def order_dir(key,value,match_obj):
            num = match_obj.groups()[0]
            orders = result[u'orders'].get(num,{})
            orders[u'dir'] = value
            result[u'orders'][num] = orders

        def draw(key,value,match_obj):
            result[u'draw'] = value

        def length(key,value,match_obj):
            result[u'length']  = value

        def start(key,value,match_obj):
            result[u'start'] = value

        patterns = [
            (u'columns\[(\d+)\]\[data\]',columns_data),
            (u'order\[(\d+)\]\[column\]',order_column),
            (u'order\[(\d+)\]\[dir\]',order_dir),
            (u'draw',draw),
            (u'length',length),
            (u'start',start)
        ]
        for key,value in request.POST.items():
            for pattern,math in patterns:
                match_obj = re.match(pattern,key)
                if match_obj:
                    math(key,value,match_obj)

        return result

class Import(object):
    @classmethod
    def import_module(cls,path):
        while len(path)>=2:
            try:
                return import_module(u'.'.join(path))
            except:
                del path[-2]

    @classmethod
    def import_model(cls,path,role,username):
        path = path.split(u'.')
        if len(path)<>3:
            raise
        full_path = [path[0],u'roles',role,username,path[1],path[2]]
        module = cls.import_module(full_path[:-1])
        return getattr(module,path[-1])

    @classmethod
    def __get_template(cls,path,file_name,default_file_name):
        '''
        :param path: Model 完整路径名称,以点分隔
        :param file_name: 需要读入的模板名称
        :param default_file_name: 如果在所有可能的路径下都无法找到对应的模板文件,会使用默认
        :return:
        '''
        path = path.split(u'.')
        path.append(file_name)
        del path[-3]
        while len(path)>=3:
            try:
                return get_template(u'/'.join(path).lower())
            except:
                del path[-3]
        return get_template(default_file_name)

    @classmethod
    def get_field_template(cls,path,file_name,default_file_name):
        return cls.__get_template(path,file_name,default_file_name)

    @classmethod
    def get_action_template(cls,path,file_name,default_file_name):
        return cls.__get_template(path,file_name,default_file_name)